package com.longrise.co.ui.mine.fragment;

import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.longrise.co.R;
import com.longrise.co.api.CommonPara;
import com.longrise.co.ui.MainFragment;
import com.longrise.co.ui.mine.contract.MineContract;
import com.longrise.co.ui.mine.model.MineModel;
import com.longrise.co.ui.mine.presenter.MinePresenter;
import com.ly.common.base.BaseFragment;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by luoyang on 2017-7-20.
 */

public class ModifyPwdFragment extends BaseFragment<MinePresenter, MineModel> implements MineContract.View<String> {


    @Bind(R.id.head_title)
    TextView headTitle;
    @Bind(R.id.old_pwd)
    EditText oldPwd;
    @Bind(R.id.eye)
    ImageView eye;
    @Bind(R.id.new_pwd)
    EditText newPwd;
    @Bind(R.id.new_eye)
    ImageView newEye;
    @Bind(R.id.confirm_new_pwd)
    EditText confirmNewPwd;
    @Bind(R.id.confirm_new_eye)
    ImageView confirmNewEye;

    private boolean isOldPwdShow = false;
    private boolean isNewPwdShow = false;
    private boolean isConfirmPwdShow = false;

    private boolean isMOdifySuccess = false;

    @Override
    protected int getLayoutResource() {
        return R.layout.fg_modify_pwd;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    protected void initView() {
        setText(headTitle, "修改密码");
    }

    @Override
    public void requestSuccess(String s) {
        showLongToast("修改密码成功");
        isMOdifySuccess = true;
    }

    @Override
    public void onRequestComplete() {
        if(isMOdifySuccess) {
          /*  Intent intent = new Intent(_mActivity, MainActivity.class);
            startActivity(intent);
            _mActivity.finish();*/
            _mActivity.popTo(MainFragment.class, false);
//            mRxManager.post(Constants.RX_EVENT_NEXT_PAGE, LoginFragment.newInstance());
//            loadRootFragment(R.id.content, LoginFragment.newInstance());
        }
    }

    public static ModifyPwdFragment newInstance() {
        return new ModifyPwdFragment();
    }

    private void change2PwdDisplayStatus(boolean isShowPwd, ImageView iv, EditText et) {
        if(iv == null || et == null) {
            return ;
        }
        int resourceId = isShowPwd ? R.drawable.eye_orange : R.drawable.eye;
        iv.setBackgroundResource(resourceId);
        et.setInputType(isShowPwd ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD : InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    }

    @OnClick({R.id.head_back, R.id.eye, R.id.new_eye, R.id.confirm_new_eye, R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.head_back:
                _mActivity.onBackPressed();
                break;
            case R.id.eye:
                isOldPwdShow = !isOldPwdShow;
                change2PwdDisplayStatus(isOldPwdShow, eye, oldPwd);
                break;
            case R.id.new_eye:
                isNewPwdShow = !isNewPwdShow;
                change2PwdDisplayStatus(isNewPwdShow, newEye, newPwd);
                break;
            case R.id.confirm_new_eye:
                isConfirmPwdShow = !isConfirmPwdShow;
                change2PwdDisplayStatus(isConfirmPwdShow, confirmNewEye, confirmNewPwd);
                break;
            case R.id.submit:
                String mobile = CommonPara.getInstance().getLoginBean().getMobile();
                mPresenter.modifyPwd(mobile, oldPwd.getText().toString(), newPwd.getText().toString(), confirmNewPwd.getText().toString());
                break;
        }
    }
}
